import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
// "动态"路由规则数组(将来我们要讲解, 这里先了解改变, 不实现代码)
// 登录的用户, 身份不同, 看见的路由页面不同->拥有的路由规则对象数量不同
// 例如: 13800000002, 超级管理员角色身份, 看到8个所有页面
// 例如: 13900002020, 财务总监角色身份, 看到(员工/工资)2个页面->左侧导航有2个->路由规则对象也只有2个
// 引入多个动态路由模块
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
// 动态路由列表
export const asyncRoutes = [
  // 1. (了解)左侧导航, 是根据路由规则数组第一层对象来动态生成左侧的导航
  // 要求每个页面, 都得对应一个一级对象
  // 总结: 每个左侧导航都分别分开对应一次layout, 页面挂载二级路由上
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

// 固定不变的, -> "静态"路由规则数组
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 联想上下文->此规则对象不在左侧导航产生导航项
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 重定向浏览器地址栏的地址变为/dashboard (地址改变会再次重新匹配路由规则数组)
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' } // 设置左侧导航项上的显示文字和字体图标名字
    }]
  },

  { // 专门用于上传表格的页面
    path: '/excel',
    component: Layout,
    hidden: true, // 不需要左侧导航
    children: [
      {
        path: '',
        component: () => import('@/views/excel')
      }
    ]
  }

  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  // routes: [...constantRoutes, ...asyncRoutes] // 静态和动态的路由规则对象合并成一个数组
  routes: [...constantRoutes]
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
